//
//  ChangePSWViewController.m
//  OneWordNew
//
//  Created by qianfeng001 on 15-7-8.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "ChangePSWViewController.h"
#import "AFNetworking.h"
@interface ChangePSWViewController ()
{
    AFHTTPRequestOperationManager *_manager;
}

@end

@implementation ChangePSWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
}
- (void)showAliertWithTitle:(NSString *)title message:(NSString *)message{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    
}
- (void)changePWDWithUrl:(NSString *)url{
    __weak typeof(self)weakSelf=self;
    NSString *myurl=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    NSLog(@"%@",token);
    NSString *oldPWD=[self.oldPassword.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *onePWD=[self.oneNewPWD.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *twoPWD=[self.twoNewPWD.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict=@{@"old_password":oldPWD,
                         @"token":token,
                         @"new_password2":twoPWD,
                         @"new_password1":onePWD,
                         @"key":@"3a4b2a12539a916c040d069ae8ac8310"
                         };
    NSLog(@"%@",dict);
    [_manager POST:myurl parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dict[@"code"]);
            if ([dict[@"code"] isEqual:@(0)]) {
                [weakSelf showAliertWithTitle:@"提示" message:@"更改成功"];
                NSUserDefaults *ud=[NSUserDefaults standardUserDefaults] ;
                [ud setObject:self.twoNewPWD.text forKey:@"PWD"];
                [ud synchronize];
                NSLog(@"密码改变成功");
                
                
            }else{
                
                [weakSelf showAliertWithTitle:@"提示" message:@"密码格式不正确"];
            }
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [ self showAliertWithTitle:@"提示" message:@"错误"];
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


- (IBAction)sentBtnClick:(UIButton *)sender {
    [self changePWDWithUrl:kChangePSWDUrl];
}
@end
